package com.sise.rsc.controller;

import com.alibaba.fastjson.JSON;
import com.sise.rsc.bean.Result;
import com.sise.rsc.entity.*;
import com.sise.rsc.repository.APackageRepository;
import com.sise.rsc.repository.CourierRepository;
import com.sise.rsc.repository.PackageActionRepository;
import com.sise.rsc.service.CourierService;
import com.sise.rsc.service.PackageService;
import com.sise.rsc.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("/courier")
public class CourierController {
    @Resource
    public CourierService courierService;
    @Resource
    private UserService userService;
    @Resource
    public PackageService packageService;
    @Resource
    public APackageRepository aPackageRepository;
    @Resource
    public CourierRepository courierRepository;
    @Resource
    public PackageActionRepository packageActionRepository;
    //请求邮差主页面
    @RequestMapping(value = "/index")
    public String courier(HttpSession session, Model model){
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        //获取用户信息

        User user = userService.findUserByUsername(auth.getName());
        int uid=user.getId();
        List<APackage> aPackageList=packageService.findAllAPackageByUserId(uid);
        session.setAttribute("aPackageList",aPackageList);
        session.setAttribute("SESSION_USER",user);
        model.addAttribute("user",user);
        return "courier/index";
    }
    //请求邮差取件页面
    @RequestMapping(value = "/fetch")
    public String  fetch(){
        return "courier/fetch";
    }
    //请求邮差派件页面
    @RequestMapping(value = "/deliver")
    public String  deliver(){
        return "courier/deliver";
    }

    //找到所有邮差
    @RequestMapping(value = "/findAllCourier")
    @ResponseBody
    public String findAllCourier(){
        List<Courier> co = courierService.findAllCourier();
        Courier cb=co.get(0);
        String string= JSON.toJSONString(cb);
        return "courier:"+string;
    }

    //查找所有包裹
    @RequestMapping(value = "/findAllAPackage")
    @ResponseBody
    public String findAllAPackage(){
        String  jsonStr = packageService.findAllAPackageToJson();
        return jsonStr;
    }
    //上传快递员揽件数据
    @RequestMapping(value = "/fetchPackage")
    @ResponseBody
    public String savePackageFetchAction(int packageId,int courierId){
        System.out.println("包裹id:"+packageId+" 收件快递员id:"+courierId);
        APackage aPackage= aPackageRepository.findAPackageById(packageId);
        Courier courier=courierRepository.findCourierById(courierId);
        PackageAction packageAction=new PackageAction("快件已经揽收", new Date() ,aPackage,courier);
        packageAction.setState(2);//设置为运输中


        APackage aPackage1 = new APackage();
        aPackage1.setId(packageId);
        aPackage1.setState(2);
        packageActionRepository.save(packageAction);
        return "包裹id为："+packageId+"的包裹揽收记录登记完成";
    }

    @RequestMapping("/getWorkingArray")
    @ResponseBody
    public Object getWorkingArray(HttpSession session){
        List<String> data= new LinkedList<>();
        User session_user = (User) session.getAttribute("SESSION_USER");
        if (session_user==null){
            return "无权限1";
        }
        if (!session_user.getRoles().get(0).getAuthority().equals("ROLE_KD"))
            return "无权限2";

        System.out.println("id:"+session_user.getId());
        Courier courier = courierRepository.findCourierByUserId(session_user.getId());
        List<CourierStatus> courierStatus = courierService.findCourierStatusesByStatusAndCourierId("正常",courier.getId());
        if (courierStatus.isEmpty())return "空";
        for (CourierStatus status : courierStatus) {
            data.add(Long.toString(status.getTime().getTime()).substring(0,10));
        }

        return data;
    }

    @RequestMapping("/state")
    public String state(){
        return "courier/state";
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Result changState(HttpSession session){

        User user = (User) session.getAttribute("SESSION_USER");
        if (user == null)
            return new Result(500,"获取用户信息失败，请重新登录后再试！");
        Courier courier = courierRepository.findCourierByUserId(user.getId());


        courierService.changStateToWorking(courier);
        return new Result();


    }



}
